#!/usr/bin/python
# -*- coding:  utf-8 -*-
import sys
import treenode
import rule

NT=0
POS=1
WD=2
class StackNode:
	def __init__(self,i,d):
		self.id = i
		self.depth = d

class Tree:
	def __init__(self,s):
		self.treestr = s
		self.nodelist = []
		self.depth = -1
		self.sentence = ""
	

	def parse(self):
		slen = len(self.treestr)
		lbc = 0
		rbc = 0
		for i in range(slen):
			if self.treestr[i] == '(':
				#if i>0 and self.treestr[i-1] == ' ':
				#	continue
				lbc += 1
			if self.treestr[i] == ')':
				#if i>0 and self.treestr[i-1] == ' ':
				#	continue
				rbc += 1
		if lbc != rbc:
			sys.stderr.write("Left/Right bracket count doesn't match.\n")
			sys.exit(-1)

		cdep = 0
		i = 0
		j = 0
		sn = []
		pos_in_sen = 0
		while i < slen:
			if self.treestr[i] == '(': # new non-terminal
				if ( j==0 ):
					j = i+1
				else:
					lb = self.treestr[j:i]
					lbclean = lb.strip()
					if len(lbclean) != 0:
						tn = treenode.TreeNode(len(self.nodelist),lbclean,cdep,0)
						self.nodelist.append(tn)
						sn.append(StackNode(tn._id,cdep))
					cdep += 1
					j = i+1
			if self.treestr[i] == ')':
				lb = self.treestr[j:i].strip()
				if lb.find(" ")>0: # word
					slw = lb.split(" ")
					try:
						while slw.index("")>0:
							slw.remove("")
					except ValueError:
						pass
					if len(slw) != 2:
						sys.stderr.write("Error in POS-Word tuple: "+lb+"\n")
						sys.exit(-1)
					pos = treenode.TreeNode(len(self.nodelist),slw[0],cdep,treenode.POS)
					self.nodelist.append(pos)
					wd = treenode.TreeNode(len(self.nodelist),slw[1],cdep+1,treenode.WD)
					wd._pos = pos_in_sen
					pos_in_sen += 1
					self.nodelist.append(wd)
					pos._child = wd._id
					wd._parent = pos._id
					sn.append(StackNode(pos._id,cdep))
				else: # no space in string, 
					if len(sn) <= 1:
						break
					fnode = sn.pop()
					while len(sn) >0:
						bnode = sn[-1]
						if bnode.depth < fnode.depth:
							self.nodelist[bnode.id]._child = fnode.id
							self.nodelist[fnode.id]._parent = bnode.id
							break
						else:
							#sys.stderr.write("ok\n")
							self.nodelist[bnode.id]._brother = fnode.id
							fnode = sn.pop()
						#if
					#while
				#if
				j = i + 1
				cdep -= 1
			#if
			i += 1
		#while
		for n in range(len(self.nodelist)):
			self.computeChildren(n)
		#self.computeHeight(0)
		#self.computeHead(0)
	

	def computeHead(self,id):
		nd = self.nodelist[id]
		lhs = nd._label
		
		if nd.isWD():
			return
			#nd._head = lhs
		if nd.isPOS():
			nd._head = self.nodelist[nd._child]._label
			return
			sys.stderr.write(nd._label+" "+ nd._head+"\n")

		ichildren = nd._children
		rhs = []
		for x in ichildren:
			self.computeHead(x)
			rhs.append((x,self.nodelist[x]._label))
		
		bdealed = False # whether this head is projected, 
										# if not, leave to printHeadError()
		if lhs == "ADJP":
			for i in rhs[::-1]:
				if i[1] == "ADJP" or i[1] == "JJ":
					bdealed = True
					nd._head = self.nodelist[i[0]]._head
					return
		elif lhs ==	 "ADVP":
			for i in rhs[::-1]:
				if i[1] == "ADVP" or i[1] == "AD":
					bdealed = True
					nd._head = self.nodelist[i[0]]._head
					return
				elif i[1] == "CS":
					bdealed = True
					nd._head = self.nodelist[i[0]]._head
					return
		elif lhs ==	 "CLP":
			for i in rhs[::-1]:
				if i[1] == "CLP" or i[1] == "M":
					bdealed = True
					nd._head = self.nodelist[i[0]]._head
					return
		elif lhs ==	 "CP":
			for i in rhs[::-1]:
				if i[1] in ["CP", "IP", "SP", "VP"]:
					bdealed = True
					nd._head = self.nodelist[i[0]]._head
					return
		elif lhs ==	 "DNP":
			bstop = False
			for i in rhs[::-1]:
				if bstop:
					bdealed = True
					nd._head = self.nodelist[i[0]]._head
					return
				if i[1] in ["DEG"]:
					bstop = Tree
		elif lhs ==	 "DP":
			nd._head = self.nodelist[rhs[-1][0]]._head
			return
			#nd._head = self.computeHead(rhs[-1][0])
		elif lhs ==	 "DVP":
			nd._head = self.nodelist[rhs[0][0]]._head
			return
			#nd._head = self.computeHead(rhs[0][0])
		elif lhs ==	 "FRAG":
			for i in rhs[::-1]:
				if i[1] in ["NN","NR","VV","IP"]:
					bdealed = True
					nd._head = self.nodelist[i[0]]._head
					return
		elif lhs ==	 "IP":
			for i in rhs[::-1]:
				if i[1] in ["VP","IP"]:
					bdealed = True
					nd._head = self.nodelist[i[0]]._head
					return
		elif lhs ==	 "LCP":
			bstop = False
			for i in rhs[::-1]:
				if bstop:
					bdealed = True
					nd._head = self.nodelist[i[0]]._head
					return
				if i[1]   == "LCP":
					bdealed = True
					nd._head = self.nodelist[i[0]]._head
					return
				elif i[1] == "LC":
					bstop = Tree
				#else: default 0
		elif lhs ==	 "LST": # only CD, OD, PU ,so default 0
			pass
		elif lhs ==	 "NP":
			for i in rhs[::-1]:
		#		sys.stderr.write(i[1]+"\n")
				if i[1] in ["NP","IP","CP","NN","QP","VP","NR","NT","PN"]:
					bdealed = True
					nd._head = self.nodelist[i[0]]._head
					return
		elif lhs ==	 "PP":
			for i in rhs[::-1]:
				if i[1] == "PP":
					bdealed = True
					nd._head = self.nodelist[i[0]]._head
					return
			bstop = False
			for i in rhs:
				if bstop:
					bdealed = True
					nd._head = self.nodelist[i[0]]._head
					return
				if i[1] == "P":
					bstop = True
		elif lhs ==	 "PRN":
			for i in rhs[::-1]:
				if rhs[-1][1] == "PU":
					bdealed = True
					nd._head = self.nodelist[rhs[-2][0]]._head
					return
					#nd._head = self.computeHead(rhs[-2][0])
			#default -1
		elif lhs ==	 "QP":
			for i in rhs[::-1]:
				if i[1] in ["QP", "CD","CLP"]:
					bdealed = True
					nd._head = self.nodelist[i[0]]._head
					return
					#return nd.setHead(self.computeHead(i[0]))
		elif lhs ==	 "UCP":
			for i in rhs[::-1]:
				if i[1] in ["NP", "IP","CLP","VP","QP","UCP"]:
					bdealed = True
					nd._head = self.nodelist[i[0]]._head
					return
					#return nd.setHead(self.computeHead(i[0]))
		elif lhs ==	 "VCD":
			for i in rhs[::-1]:
				if i[1] in ["VA", "VV","VC"]:
					bdealed = True
					nd._head = self.nodelist[i[0]]._head
					return
					#return nd.setHead(self.computeHead(i[0]))
		elif lhs ==	 "VP":
			for i in rhs[::-1]:
				if i[1] in ["VA", "VV","VC","VCD","VE","VRD","VSB"]:
					bdealed = True
					nd._head = self.nodelist[i[0]]._head
					return
					#return nd.setHead(self.computeHead(i[0]))
				if i[1] in ["ADJP","VP","QP","IP","CP","LCP"]:
					bdealed = True
					nd._head = self.nodelist[i[0]]._head
					return
					#return nd.setHead(self.computeHead(i[0]))
				if i[1] in ["PP", "NP"]:
					bdealed = True
					nd._head = self.nodelist[i[0]]._head
					return
					#return nd.setHead(self.computeHead(i[0]))
		elif lhs in [ "ROOT","VPT","VRD","VSB","LST" ]:
			nd._head = self.nodelist[rhs[-1][0]]._head
			return
			#nd._head = self.computeHead(rhs[-1][0])
		#default : LST, VPT, VRD, VSB	
		if not bdealed:
			sys.stderr.write("Warning: Defalut action taken in projecting head: "+self.getSimpleRule(id).toString()+"\n")
			nd._head = self.nodelist[rhs[-1][0]]._head
			return
			#return nd.setHead(self.computeHead(rhs[-1][0]))
		

	def computeHeight(self,id):
		chid = self.nodelist[id]._child
		if (chid < 0):
			self.nodelist[id]._height = 0
			return 0
		else:
			allc = []
			allc.append(chid)
			while True:
				bid = self.nodelist[chid]._brother
				if bid >=0:
					allc.append(bid)
					chid = bid
				else:
					break
			maxheight = 0;
			for i in range(len(allc)):
				height = self.computeHeight(allc[i])
				if height > maxheight:
					maxheight = height
			self.nodelist[id]._height = maxheight+1 
			return maxheight + 1

	def computeChildren(self,n):
		if len(self.nodelist) <= n:
			sys.stderr.write("node "+str(n)+" out of boundery.\n")
			return
		if self.nodelist[n].isWD():
			return
		ch = self.nodelist[n]._child
		self.nodelist[n].addChild(ch)
		while ch > 0:
			ch = self.nodelist[ch]._brother
			if ch>0:
				self.nodelist[n].addChild(ch)

	def generateRules(self,bAddPOSRule,bHeaded):
		if len(self.nodelist) <=0:
			sys.stderr.write("Please parse the tree first.\n")
			return None 
		rules = []
		heads = []
		heads.append(0)
		while len(heads)>0:
			h = heads.pop(0)
			#sys.stderr.write(str(len(heads))+" "+str(h)+"\n")
			chs = self.nodelist[h]._children
			if chs == None:
				continue
			for c in chs:
				heads.append(c)
			#sys.stderr.write(self.getRuleHeadedWith(h).toString()+"\n")
			if self.nodelist[h].isNT():
				if bHeaded:
					rules.append(self.getHeadedRule(h))
				else:
					rules.append(self.getSimpleRule(h))
			if bAddPOSRule and self.nodelist[h].isPOS():
				if bHeaded:
					rules.append(self.getHeadedRule(h))
				else:
					rules.append(self.getSimpleRule(h))
		return rules

	def getHeadedRule(self,n):
		if n>= len(self.nodelist):
			sys.stderr.write("node "+str(n)+" out of boundery.\n")
			return None
		r = rule.HeadedRule()
		nd = self.nodelist[n]
		r._lhs = rule.RuleNode(0,nd._label,nd._head)
		chs = self.nodelist[n]._children
		for i,c in enumerate(chs):
			cnd = self.nodelist[c]
			r.addRhs(rule.RuleNode(i,cnd._label,cnd._head))
		return r

	def getSimpleRule(self,n):
		if n>= len(self.nodelist):
			sys.stderr.write("node "+str(n)+" out of boundery.\n")
			return None
		r = rule.SimpleRule()
		r._lhs = self.nodelist[n]._label
		chs = self.nodelist[n]._children
		for c in chs:
			r.addRhs(self.nodelist[c]._label)
		return r
		

	def toSentence(self):
		words = []
		for n in self.nodelist:
			if n.isWD():
				words.append(n._label)
		return " ".join(words)

	def toString(self):
		s="ID\tLabel\tParent\tChild\tBrother\tDepth\tHeight\tType\tPos\tChildren"
		lb = []
		lb.append(s)
		for i in range(len(self.nodelist)):
			lb.append(self.nodelist[i].toString())
		return "\n".join(lb)
		 
	def toSimpleString(self):
		s="ID\tLabel\tHead\tType\tParent\tChildren"
		lb = []
		lb.append(s)
		for i in range(len(self.nodelist)):
			lb.append(self.nodelist[i].toSimpleString())
		return "\n".join(lb)

if __name__ == "__main__":
	#pt = Tree("( (IP-HLN (NP-SBJ (NP-PN (NR 上海) (NR 浦东))  (NP (NN 开发) (CC 与) (NN 法制) (NN 建设))) (VP (VV 同步))) ) ")
	pt = Tree(" (ROOT (IP (NP (NP (NR 上海) (NR 浦东)) (NP (NN 开发))) (VP (PP (P 与) (NP (NN 法制) (NN 建设))) (VP (VV 同步))))) ")
	pt.parse()
	sys.stderr.write(pt.toSimpleString()+"\n")
	#sys.stderr.write(pt.toString()+"\n")

"""
(ROOT (IP (NP (ADJP (JJ 上述)) (QP (CD 两) (CLP (M 所))) (NP (NN 太学))) (VP (VC 是) (NP (CP (IP (VP (PP (P 在) (LCP (NP (DNP (NP (NP (NN 国家) (NN 体委)) (PU 、) (NP (NN 文化部)) (PU 、) (NP (NR 中国) (NN 武术) (NN 研究院)) (PU 、) (NP (NP (NR 中国)) (NP (NN 艺术) (NN 研究院)))) (DEG 的)) (NP (NN 关杯) (CC 和) (NN 支持))) (LC 下))) (PU ，) (VP (PP (P 在) (LCP (IP (NP (NP (NR 台湾)) (ADJP (JJ 著名)) (NP (NP (NN 企业家) (PU 、) (NN 书画家) (PU 、) (NN 艺术品) (NN 收藏家)) (NP (NR 李志仁)) (NP (NN 先生)))) (VP (VV 倡议) (CC 和) (VV 出资))) (LC 下))) (PU ，) (PP (P 经) (IP (NP (NP (NN 国家) (NN 教委)) (CC 和) (NP (NP (NR 北京市)) (NP (NN 成人) (NN 教育局)))) (VP (VV 批准)))) (VP (MSP 而) (VP (VV 成立)))))) (DEC 的)))) (PU 。)))
(ROOT (FRAG (PU （) (VV 完) (PU ）)))
(ROOT (FRAG (NR 新华社) (NR 昌平) (NT ９月) (NT １日) (NN 电)))
(ROOT (IP (NP (NP (DP (DT 全)) (NP (NN 国))) (NP (NN 赛车场) (NN 自行车) (NN 锦标赛))) (VP (NP (NT 今天)) (VP (VV 进入) (QP (OD 第二) (CLP (M 天))))) (PU 。)))
(ROOT (IP (NP (DNP (NP (NR 辽宁队)) (DEG 的)) (NP (NR 郭龙臣))) (VP (VP (PP (P 在) (LCP (NP (DNP (NP (QP (CD ４) (CLP (M 公里))) (NP (NN 个人) (NN 追逐赛))) (DEG 的)) (NP (NN 决赛))) (LC 中))) (PU ，) (PP (P 以) (QP (QP (CD ４) (CLP (M 分))) (QP (CD ４４) (CLP (M 秒))) (QP (CD ８２)))) (PU ，) (ADVP (AD 再)) (VP (VV 破) (NP (NP (DP (DT 全)) (NP (NN 国))) (NP (NN 纪录))))) (PU ，) (CC 并) (VP (VV 夺得) (NP (DNP (NP (DP (DT 这) (CLP (M 个))) (NP (NN 项目))) (DEG 的)) (NP (NN 金牌))))) (PU 。)))
(ROOT (IP (NP (NR 郭龙臣)) (VP (NP (NT 昨天)) (PP (P 在) (LCP (NP (DNP (NP (DP (DT 这) (CLP (M 个))) (NP (NN 项目))) (DEG 的)) (NP (NN 资格赛))) (LC 中))) (PP (P 以) (NP (DNP (QP (QP (CD ４) (CLP (M 分))) (QP (CD ４６) (CLP (M 秒))) (QP (CD ７７))) (DEG 的)) (NP (NN 成绩)))) (VP (VV 打破) (AS 了) (NP (CP (IP (NP (PN 他) (PN 本人)) (VP (PP (P 在) (LCP (NP (NN 七运会)) (LC 上))) (VP (VV 创造)))) (DEC 的)) (DNP (QP (QP (CD ４) (CLP (M 分))) (QP (CD ４９) (CLP (M 秒))) (QP (CD ７０))) (DEG 的)) (NP (DP (DT 全)) (NP (NN 国))) (NP (NN 纪录))))) (PU 。)))
(ROOT (IP (NP (NP (NR 北京队) (NR 汤学忠)) (CC 和) (NP (DNP (NP (NR 辽宁队)) (DEG 的)) (NP (NR 潘广春)))) (VP (ADVP (AD 分)) (VP (VV 获) (QP (DNP (NP (DP (DT 这) (CLP (M 个))) (NP (NN 项目))) (DEG 的)) (QP (QP (OD 第二)) (PU 、) (QP (CD 三) (CLP (M 名))))))) (PU 。)))
(ROOT (IP (PP (P 在) (LCP (NP (NP (NN 女子)) (QP (CD ３) (CLP (M 公里))) (NP (NN 个人) (NN 追逐赛) (NN 决赛))) (LC 中))) (PU ，) (NP (DNP (NP (NR 河北队)) (DEG 的)) (NP (NR 郭杏红))) (VP (PP (P 以) (NP (DNP (QP (QP (CD ３) (CLP (M 分))) (QP (CD ５７) (CLP (M 秒))) (QP (CD ６６５))) (DEG 的)) (NP (NN 成绩)))) (VP (VV 夺得) (NP (NN 金牌)))) (PU 。)))
(ROOT (IP (NP (NP (NR 吉林队) (NR 赵海娟)) (CC 和) (NP (NR 河南队) (NR 马惠珍))) (VP (ADVP (AD 分)) (VP (VV 获) (QP (QP (OD 第二)) (PU 、) (QP (CD 三) (CLP (M 名)))))) (PU 。)))
(ROOT (FRAG (NR 新华社) (NR 汉城) (NT ８月) (NT ３１日) (NN 电) (PU （) (NN 记者) (NR 汤水富) (PU ）)))
(ROOT (IP (NP (QP (OD 首) (CLP (M 届))) (NP (PU “) (NR 乐天杯) (PU ”) (NR 中) (NR 韩) (NN 围棋) (NN 对抗赛))) (VP (NP (NT 今天)) (PP (P 在) (NP (NP (NP (NR 韩国)) (ADJP (QP (OD 第二)) (ADJP (JJ 大))) (NP (NN 城市))) (NP (NR 釜山)))) (VP (VV 拉开) (NP (NN 帷幕)))) (PU 。)))
(ROOT (IP (NP (NR 中国队)) (VP (PP (P 在) (LCP (NP (QP (OD 第一) (CLP (M 场))) (NP (NN 比赛))) (LC 中))) (PP (P 以) (QP (CD ４) (PU ：) (CD ３))) (VP (VV 战胜) (AS 了) (NP (CP (IP (NP (NN 实力)) (VP (ADVP (AD 较)) (VP (VA 强)))) (DEC 的)) (NP (NR 韩国队))))) (PU 。)))
(ROOT (IP (PP (P 在) (LCP (NP (DNP (NP (NT 今天)) (DEG 的)) (NP (NN 比赛))) (LC 中))) (PU ，) (NP (PN 双方)) (VP (ADVP (AD 各)) (VP (VE 有) (IP (NP (QP (CD ７) (CLP (M 名))) (NP (NN 棋手))) (VP (VV 进行) (NP (NN 角逐)))))) (PU 。)))
(ROOT (IP (NP (DNP (NP (NR 中国队)) (DEG 的)) (NP (NP (NP (NR 聂卫平)) (QP (CD 九) (CLP (M 段)))) (PU 、) (NP (NP (NR 刘小光)) (QP (CD 九) (CLP (M 段)))) (PU 、) (NP (NP (NR 常昊)) (QP (CD 六) (CLP (M 段)))) (CC 和) (NP (NP (NR 曹大元)) (QP (CD 九) (CLP (M 段)))))) (VP (ADVP (AD 分别)) (VP (VV 战胜) (NP (DNP (NP (NR 韩国队)) (DEG 的)) (NP (NP (NP (NR 崔（圭加王旁）（丙加日旁）)) (QP (CD 六) (CLP (M 段)))) (PU 、) (NP (NP (NR 张秀英)) (QP (CD 九) (CLP (M 段)))) (PU 、) (NP (NP (NR 徐奉洙)) (QP (CD 九) (CLP (M 段)))) (CC 和) (NP (NP (NR 林宣根)) (QP (CD 八) (CLP (M 段)))))))) (PU 。)))
(ROOT (IP (NP (NN 其中)) (NP (NR 聂卫平) (PU 、) (NR 刘小光) (CC 和) (NR 常昊)) (VP (ADVP (AD 均)) (PP (P 在) (NP (NN 中盘))) (VP (VV 取胜))) (PU 。)))
(ROOT (IP (NP (NR 曹大元)) (VP (PP (P 以) (NP (DNP (QP (QP (CD 七) (CLP (M 目))) (QP (CD 半))) (DEG 的)) (NP (NN 优势)))) (VP (VV 获胜))) (PU 。)))
(ROOT (IP (NP (NP (NP (NP (NR 中国)) (NP (NN 代表团))) (NP (NN 团长))) (NP (NR 王汝南))) (VP (PP (P 在) (LCP (IP (VP (VV 接受) (NP (DNP (NP (NN 记者)) (DEG 的)) (NP (NN 电话) (NN 采访))))) (LC 时))) (VP (VV 介绍) (PU ，) (IP (IP (VP (PP (P 从) (NP (NN 整体))) (VP (MSP 来) (VP (VV 说))))) (PU ，) (IP (NP (DNP (NP (NR 韩国队)) (DEG 的)) (NP (NN 实力))) (VP (VV 要) (VP (VV 强于) (NP (NR 中国队)))))))) (PU 。)))
(ROOT (IP (IP (ADVP (AD 但)) (NP (NT 今天)) (NP (NR 中国队)) (VP (NP (NN 发挥)) (ADVP (AD 较)) (VP (VA 好)))) (PU ，) (IP (VP (ADVP (AD 因此)) (VP (VV 获得) (AS 了) (NP (NN 胜利))))) (PU 。)))
(ROOT (IP (NP (NT 以前)) (NP (PN 他)) (VP (VP (PP (P 与) (NP (NP (NR 中国)) (NP (NN 棋手)))) (VP (VV 下) (AS 过) (QP (CD 九) (CLP (M 盘))))) (PU ，) (VP (ADVP (AD 全)) (ADVP (AD 都)) (VP (VV 获胜)))) (PU 。)))
(ROOT (IP (IP (NP (NP (NP (NR 中) (NR 韩)) (QP (CD 两)) (NP (NN 国))) (NP (NN 围棋队))) (VP (ADVP (AD 将)) (PP (P 于) (NP (NT ９月) (NT １日))) (VP (VV 移师) (NP (NR 汉城))))) (PU ，) (IP (NP (NT ２日)) (VP (PP (P 在) (NP (NP (NR 乐天)) (NP (NN 饭店)))) (VP (VV 举行) (NP (QP (OD 第二) (CLP (M 场))) (NP (NN 比赛)))))) (PU 。)))
(ROOT (IP (PP (P 根据) (NP (CP (IP (NP (PN 双方)) (VP (VV 商定))) (DEC 的)) (NP (NN 规则)))) (PU ，) (CP (ADVP (CS 如果)) (IP (QP (CD 两) (CLP (M 队))) (VP (PP (P 以) (QP (CD ７) (PU ：) (CD ７))) (VP (VV 战平))))) (PU ，) (ADVP (AD 那么)) (VP (ADVP (AD 最后)) (VP (VV 要) (VP (VP (VV 看) (NP (DNP (NP (NP (DNP (NP (NR 中国队)) (DEG 的)) (NP (NR 聂卫平))) (CC 和) (NP (DNP (NP (NR 韩国队)) (DEG 的)) (NP (NR 曹薰铉)))) (DEG 的)) (NP (NN 比赛) (NN 结果)))) (VP (MSP 来) (VP (VV 决定) (NP (NN 胜负))))))) (PU 。)))
(ROOT (IP (IP (NP (NP (DP (DT 本) (CLP (M 届))) (NP (NN 比赛))) (NP (NN 奖金))) (VP (VC 为) (QP (CD １０万) (CLP (M 美元))))) (PU ，) (IP (NP (CP (IP (VP (VV 获胜))) (DEC 的)) (NP (NN 队))) (VP (VV 可) (VP (VV 得到) (QP (CD ７万) (CLP (M 美元)))))) (PU ，) (IP (ADVP (AD 而)) (NP (CP (IP (VP (VV 失利))) (DEC 的)) (NP (NN 队))) (VP (VV 可) (VP (VV 获得) (QP (CD ３万) (CLP (M 美元)))))) (PU 。)))
(ROOT (FRAG (PU （) (VV 完) (PU ）)))
(ROOT (FRAG (NR 新华社) (NR 北京) (NT ９月) (NT １日) (NN 电)))
(ROOT (IP (NP (NN 记者)) (VP (NP (NR 李贺普)) (PP (P 从) (NP (NP (NP (QP (OD 第六) (CLP (M 届))) (NP (NP (NR 远东)) (CC 及) (NP (NP (NR 南太平洋)) (NP (NN 地区)))) (NP (NN 残疾人) (NN 运动会))) (NP (NN 组委会))) (NP (NN 外联部)))) (VP (VV 获悉) (PU ：) (IP (PP (P 截止) (NP (NT 今晚))) (PU ，) (NP (DNP (NP (CP (IP (VP (VV 参加) (NP (DP (DT 本) (CLP (M 届))) (NP (NN 运动会))))) (DEC 的)) (QP (CD ３１) (CLP (M 个))) (NP (NN 国家) (CC 和) (NN 地区))) (DEG 的)) (NP (NN 代表团))) (VP (VP (ADVP (AD 已)) (VP (VV 抵达) (NP (NR 北京)))) (PU ，) (CC 并) (VP (VV 进驻) (NP (NN 运动员村))))))) (PU 。)))
(ROOT (IP (NP (NP (NN 东道主)) (NP (NR 中国)) (NP (NN 体育) (NN 代表团))) (VP (ADVP (AD 已)) (PP (P 于) (NP (NT ８月) (NT ２５日))) (VP (VV 进驻) (NP (NN 运动员村)))) (PU 。)))
(ROOT (IP (PP (P 据) (NP (NN 了解))) (PU ，) (NP (DP (DT 这些)) (NP (NN 代表团))) (VP (VP (PP (P 在) (NP (NP (NN 首都) (NN 机场)) (PU 、) (NP (NP (NN 注册)) (ADJP (JJ 制证)) (NP (NN 中心))) (CC 和) (NP (NN 运动村)))) (PU ，) (ADVP (AD 都)) (VP (VV 受到) (AS 了) (NP (DNP (NP (ADJP (JJ 志愿)) (NP (NN 人员))) (DEG 的)) (CP (IP (VP (VCD (VA 热情) (VA 周到)))) (DEC 的)) (NP (NN 接待))))) (PU ，) (VP (PP (PP (P 从) (IP (VP (VV 出关)))) (PP (P 到) (IP (VP (VV 进驻) (NP (NN 运动员村)))))) (ADVP (AD 均)) (ADVP (AD 十分)) (VP (VA 顺利)))) (PU 。)))
(ROOT (FRAG (PU （) (VV 完) (PU ）)))
(ROOT (IP (NP (NP (QP (OD 第七) (CLP (M 届))) (NP (NN 世界) (NN 游泳) (NN 锦标赛))) (NP (NP (NN 男子)) (QP (CD １) (CLP (M 米))) (NP (NN 跳板))) (NP (NN 预赛))) (VP (NP (NT 今天) (NT 上午)) (PP (P 在) (NP (PN 这里))) (VP (VV 举行))) (PU 。)))
(ROOT (IP (NP (NP (NP (NR 中国)) (NP (NN 选手))) (NP (NR 兰卫) (CC 和) (NR 陈晟))) (VP (ADVP (AD 双双)) (VP (VV 获得) (AS 了) (NP (NN 复赛) (NN 资格)))) (PU 。)))
(ROOT (IP (PP (P 在) (LCP (NP (NN 预赛)) (LC 中))) (PU ，) (NP (CP (IP (IP (VP (VV 来自) (NP (NR 广东)))) (PU 、) (IP (NP (NT 今年)) (VP (QP (CD ２６) (CLP (M 岁)))))) (DEC 的)) (NP (NR 兰卫))) (VP (VP (VV 身手不凡)) (PU ，) (VP (PP (P 以) (QP (NP (ADJP (JJ 总)) (NP (NN 成绩))) (QP (CD ３５５·３５) (CLP (M 分))))) (VP (VV 居) (NP (ADJP (JJ 领先)) (NP (NN 地位)))))) (PU 。)))
(ROOT (IP (IP (NP (NP (CP (IP (VP (VV 来自) (NP (NR 湖北)))) (DEC 的)) (NP (NN 选手))) (PU 、) (NP (DNP (QP (NP (NT 今年)) (QP (CD １９) (CLP (M 岁)))) (DEG 的)) (NP (NR 陈晟)))) (VP (ADVP (AD 暂)) (VP (VV 居) (QP (OD 第六) (CLP (M 位)))))) (PU ，) (IP (NP (ADJP (JJ 总)) (NP (NN 成绩))) (VP (VC 为) (QP (CD ３４２·６０) (CLP (M 分))))) (PU 。)))
(ROOT (IP (IP (NP (CP (IP (VP (ADVP (AD 暂)) (VP (VV 居) (QP (OD 第二) (CLP (M 位)))))) (DEC 的)) (NP (NN 选手))) (VP (VC 是) (NP (NP (NP (NR 德国)) (NP (NN 运动员))) (NP (NR 鲍·利佐))))) (PU ，) (IP (NP (ADJP (JJ 总)) (NP (NN 成绩))) (VP (VP (VC 为) (QP (CD ３５５·０２) (CLP (M 分)))) (PU ，) (VP (ADVP (AD 略)) (VP (VV 低于) (NP (NR 兰卫)))))) (PU 。)))
(ROOT (IP (NP (DNP (NP (CP (IP (VP (ADVP (AD 暂)) (VP (VV 居) (QP (OD 第三) (CLP (M 位)))))) (DEC 的)) (NP (NP (NR 津巴布韦)) (NP (NN 选手))) (NP (NR 斯·埃万))) (DEG 的)) (ADJP (JJ 总)) (NP (NN 成绩))) (VP (VC 为) (QP (CD ３５４·３９) (CLP (M 分)))) (PU 。)))
(ROOT (IP (LCP (NP (NN 赛)) (LC 后)) (PU ，) (NP (NP (NP (NP (NR 中国)) (NP (NN 跳水队))) (NP (NN 总教练))) (NP (NR 徐益明))) (VP (PP (P 对) (NP (NP (DP (DT 本)) (NP (NN 社))) (NP (NN 记者)))) (VP (VV 说) (PU ：) (IP (PU “) (NP (NR 陈晟)) (VP (NP (NT 今天)) (ADVP (AD 有些)) (VP (VA 紧张)))))) (PU 。) (PU ”)))
(ROOT (IP (NP (PN 他)) (VP (ADVP (AD 还)) (VP (VV 说) (PU ：) (IP (PU ”) (NP (DNP (NP (PN 我们)) (DEG 的)) (NP (NN 运动员))) (VP (PP (P 对) (IP (VP (PP (P 在) (LCP (NP (ADJP (JJ 强)) (NP (NN 阳光))) (LC 下))) (VP (VV 进行) (NP (NN 比赛)))))) (ADVP (AD 不)) (ADVP (AD 太)) (VP (VV 适应)))))) (PU 。)))
(ROOT (IP (PP (P 在) (NP (NN 国内))) (NP (PN 他们)) (VP (VP (VV 进行) (NP (NN 室内) (NN 训练))) (VP (ADVP (AD 比较)) (VP (VA 多)))) (PU 。)))
(ROOT (IP (ADVP (AD 看来)) (PU ，) (VP (ADVP (AD 还)) (VP (VV 要) (VP (ADVP (AD 多)) (PP (P 在) (NP (NN 室外))) (VP (VV 训练))))) (PU 。) (PU ”)))
(ROOT (IP (NP (PN 他)) (VP (VV 认为) (PU ，) (IP (NP (DP (DT 这) (CLP (M 次))) (NP (NN 比赛))) (NP (NP (NP (PN 我)) (NP (NN 国))) (CP (IP (VP (VV 参赛))) (DEC 的)) (ADJP (JJ 新)) (NP (NN 选手))) (VP (VA 多)))) (PU 。)))
(ROOT (IP (NP (PN 他们)) (VP (ADVP (AD 还)) (VP (VV 缺乏) (NP (DNP (NP (NN 国际) (NN 比赛)) (DEG 的)) (NP (NN 经验))))) (PU 。)))
(ROOT (IP (NP (NP (NN 女子) (NN 跳水)) (QP (CD １０) (CLP (M 米))) (NP (NN 跳台) (NN 预赛))) (VP (ADVP (AD 将)) (PP (P 在) (NP (NT 今天) (NT 下午))) (VP (VV 举行))) (PU 。)))
(ROOT (IP (NP (NP (NP (PN 我)) (NP (NN 国))) (NP (NP (CP (IP (VP (ADVP (AD 曾)) (VP (VV 获) (NP (NP (NP (NN 世界) (NN 锦标赛)) (NP (NN 冠军))) (CC 和) (NP (NP (NR 奥运会)) (NP (NN 冠军))))))) (DEC 的)) (NP (NP (NR 湖北)) (NP (NN 选手))) (NP (NR 伏明霞))) (CC 和) (NP (CP (IP (VP (ADVP (AD 曾)) (VP (VV 获) (NP (NP (NR 世界杯)) (NP (NN 冠军)))))) (DEC 的)) (NP (NP (NR 河北)) (NP (NN 选手))) (NP (NR 池彬))))) (VP (ADVP (AD 将)) (VP (VV 参加) (NP (NN 比赛)))) (PU 。)))
(ROOT (FRAG (PU （) (VV 完) (PU ）)))
(ROOT (FRAG (NR 新华社) (NR 罗马) (NT ９月) (NT １日) (NN 电) (PU （) (NN 记者) (NR 黄昌瑞) (NR 杨爱国) (PU ）)))
(ROOT (IP (IP (NP (NP (NP (NP (NR 中国)) (NP (NN 跳水队))) (ADJP (JJ 女)) (NP (NN 选手))) (NP (NR 伏明霞) (CC 和) (NR 池彬))) (VP (NP (NT 今天) (NT 下午)) (PP (P 在) (LCP (NP (DNP (QP (NP (CP (IP (NP (PN 这里)) (VP (VV 举行))) (DEC 的)) (QP (OD 第七) (CLP (M 届))) (NP (NN 世界) (NN 游泳) (NN 锦标赛))) (NP (NP (NN 女子)) (QP (CD １０) (CLP (M 米))) (NP (NN 跳台) (NN 预赛))) (DP (DT 前) (QP (CD ６) (CLP (M 轮))))) (DEG 的)) (NP (NN 比赛))) (LC 中))) (NP (NN 成绩)) (VP (VA 理想)))) (PU ，) (IP (IP (VP (VV 进入) (NP (NN 复赛)))) (VP (ADVP (AD 已经)) (VP (VV 在望)))) (PU 。)))
(ROOT (IP (PP (P 截止) (LCP (IP (NP (NN 记者)) (VP (VV 发稿))) (LC 时))) (PU ，) (PP (P 在) (LCP (NP (DNP (DP (DT 前) (QP (CD ６) (CLP (M 轮)))) (DEG 的)) (NP (NN 角逐))) (LC 中))) (PU ，) (NP (NR 伏明霞) (CC 和) (NR 池彬)) (VP (ADVP (AD 分别)) (PP (P 以) (QP (NP (NN 总分)) (QP (CD ３３９·１２)) (CC 和) (QP (CD ２９０·１９) (CLP (M 分))))) (ADVP (AD 暂时)) (VP (VV 居) (NP (DNP (QP (QP (OD 第一)) (PU 、) (QP (OD 第三) (CLP (M 名)))) (DEG 的)) (NP (NN 位置))))) (PU 。)))
(ROOT (IP (CP (ADVP (CS 若)) (IP (VP (ADVP (AD 不)) (VP (VV 出现) (NP (NN 意外)))))) (PU ，) (NP (PN 她们)) (VP (VP (VV 进入) (NP (NN 复赛))) (VP (ADVP (AD 已经)) (VP (VV 在望)))) (PU 。)))
(ROOT (IP (NP (CP (IP (VP (VV 居) (QP (OD 第二) (CLP (M 位))))) (DEC 的))) (VP (VC 是) (NP (NP (NP (NR 美国)) (NP (NN 选手))) (NP (NR 里查德)))) (PU 。)))
(ROOT (IP (NP (DNP (NP (PN 她)) (DEG 的)) (NP (NN 成绩))) (VP (VC 是) (QP (CD ３０３·００) (CLP (M 分)))) (PU 。)))
(ROOT (IP (PP (P 在) (LCP (NP (DNP (NP (NT 今天) (NT 上午)) (DEG 的)) (NP (NP (NN 男子)) (QP (CD １) (CLP (M 米))) (NP (NN 跳板))) (NP (NN 预赛))) (LC 中))) (PU ，) (NP (NP (NP (NP (PN 我)) (NP (NN 国))) (NP (NN 选手))) (NP (NR 兰卫) (CC 和) (NR 陈晟))) (VP (ADVP (AD 已)) (VP (VV 取得) (NP (NN 复赛权)))) (PU 。)))
(ROOT (IP (NP (CP (IP (VP (VV 参赛))) (DEC 的))) (VP (ADVP (AD 共)) (VP (VE 有) (NP (QP (CD １２) (CLP (M 支))) (NP (NN 球队))))) (PU 。)))
(ROOT (FRAG (PU （) (VV 完) (PU ）))) 
"""
